package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品属性
 *
 * @author dengwei
 * @email dengwei@gmail.com
 * @date 2024-04-06 03:17:30
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 修改商品规格
     * POST
     * /product/attr/update/{spuId}
     * @param spuId
     * @return
     */
    @RequestMapping("/update/{spuId}") //@RequestMapping默认是GET请求
    //@RequiresPermissions("product:attr:list")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities){
        this.productAttrValueService.updateSpuAttr(spuId, entities);

        return R.ok();
    }

    /**
     * 获取spu规格
     * GET /product/attr/base/listforspu/{spuId}
     * @param spuId
     * @return
     */
    @RequestMapping("/base/listforspu/{spuId}") //@RequestMapping默认是GET请求
    //@RequiresPermissions("product:attr:list")
    public R listForSpuInfo(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = this.productAttrValueService.listForSpuInfo(spuId);

        return R.ok().put("data", data);
    }

    /**
     * 查询规格参数
     * http://localhost:88/api/product/attr/base/list/225?t=1719793099150&page=1&limit=10&key=
     * 查询销售属性
     * http://localhost:88/api/product/attr/sale/list/225?t=1719793099150&page=1&limit=10&key=
     *
     * attrType: base-规格参数，sale-销售属性
     *
     * @param params
     * @param catelogId
     * @return
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    //@RequiresPermissions("product:attr:delete")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String attrType){
            PageUtils page = this.attrService.baseList(params, catelogId, attrType);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		// AttrEntity attr = attrService.getById(attrId);
		AttrRespVo attr = attrService.getAttr(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存规格参数
     * 保存销售属性
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		this.attrService.saveAttrVo(attr);

        return R.ok();
    }

    /**
     * 修改规格参数
     * 修改销售属性
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		this.attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
